<?php
class PassportModel extends AbstractModel
{
	public function __construct()
	{
		parent::__construct('users');
	}

	protected function _setFields()
	{
		return array(
			'AID',
			'UserName',
			'Password',
			'Salt',
			'Status',
			'CreateDate',
			'Remark'
		);
	}

	protected function _setPrimaryKeys()
	{
		return array(
			'AID'
		);
	}

	protected function _setAutoIncrementFields()
	{
		return array(
			'AID'
		);
	}

	protected function _setForeignKeyRelation()
	{
		return array();
	}

	public function getUserInfoByUid($uid, $fields = '*')
	{
	    $uid = intval($uid);
	    if(empty($uid)) return array();
	    $sql = "SELECT {$fields} FROM `{$this->_table}` WHERE `AID`={$uid}";
	    $slave = DB::getSlave('Passport');
	    return $slave->getRow($sql);
	}
	
    public function isLogined()
	{
	    $uid = Functions::gpc(Config::getConfig('COOKIEPRE', 'Passport').'uid', 'c', 0);
	    if(empty($uid)) return false;
	    $userInfo = $this->getUserInfoByUid($uid);
	    if(empty($userInfo) || $userInfo['Status'] != 1) return false;
	    //数据库中获取用户信息
	    $username = $userInfo['UserName'];
	    $md5Password = $userInfo['Password'];
	    if($this->getToken($uid, $username, $md5Password) != Functions::gpc(Config::getConfig('COOKIEPRE', 'Passport').'token', 'c', ''))
	    {
	        return false;
	    }
	    return $uid;
	}

	private function getToken($uid, $username, $md5Password)
	{
	    return md5($md5Password.$uid.Config::getConfig('TOKEN_SALT', 'Passport').$username);
	}

	protected function _validationField($fieldName, $value) 
	{
		return true;
	}
}